<?php
namespace Admin\Model;
use Think\Model;

/**
* Created by hanquan.
* User administrator
* Date 2017/4/12 
* Time 10:55
* RecycleOrderModel 回收订单类
*/
class RecycleOrderModel extends Model{

	private $RecycleOrder = null;
	function __construct(){
		$this->RecycleOrder = M('RecycleOrder');
	}

	/**
	 * 获取回收订单数
	 * @param int $status 订单状态，-1-未完成，0-待处理，1-待接单，2-已接单，3-待确认， 4-客户取消订单，5-待回收，6-已回收，7-已完成
	 * @return int 回收订单数
	 */
	public function get_order_count($re_id = FALSE,$status = FALSE,$member_id=FALSE){
		$condition = array();
		if($re_id){
			$condition['region'] = $re_id;
		}
		if($status !== FALSE){
			$condition['status'] = $status;
		}else{
			$condition['status'] = array('egt',0);
		}
		if($member_id){
			$condition['mid'] = $member_id;
		}
		$recycle_num = $this->RecycleOrder->where($condition)->count();
		$condition = array();
		$condition['admin_id'] = $member_id;
		$condition['status'] = array('egt',2);
		$maintain_num = M('order')->where($condition)->count();
		return $recycle_num + $maintain_num;
	}


	public function get_order_count2($member_id){
		$condition = array();
		if($member_id){
			$condition['mid'] = $member_id;
		}
		$recycle_num = $this->RecycleOrder->where($condition)->count();
		$condition = array();
		$condition['admin_id'] = $member_id;
		$condition['status'] = array('egt',2);
		$maintain_num = M('order')->where($condition)->count();
		$data['recycle_num'] = $recycle_num;
		$data['maintain_num'] = $maintain_num;
		return $data;
	}

	/**
	 * 获取回收订单列表
	 * @param  int $status 订单状态，-1-未完成，0-待处理，1-待接单，2-已接单，3-待确认， 4-客户取消订单，5-待回收，6-已回收，7-已完成
	 * @return array 二维数组
	 */
	public function get_order_list($status = FALSE,$re_id = FALSE,$repair_way=FALSE){
		$condition = array();
		if($re_id){
			$condition['mp_recycle_order.region'] = $re_id;
		}
		if($status != ''){
			$condition['status'] = $status;
		}else{
			$condition['status'] = array('egt',0);
		}
		if($repair_way){
			$condition['repair_way'] = $repair_way;
		}
		return $this->RecycleOrder
		->join('mp_region ON mp_recycle_order.region=mp_region.re_id','LEFT')
		->join('mp_version ON mp_recycle_order.vid=mp_version.vid','LEFT')
		->join('mp_brand ON mp_version.bid=mp_brand.bid','LEFT')
		->join('mp_member ON mp_recycle_order.mid=mp_member.member_id','LEFT')
		->join('mp_city ON mp_region.city_id=mp_city.city_id','LEFT')
		->field('rid,status,order_num,update_time,valuation,price,phone_number,mp_region.re_id,mp_city.city_id,mp_region.region,address,door_time,b_name,v_name,mp_info,m_name,m_type,m_account')
		->where($condition)->order('rid desc')->select();
	}

	/**
	 * 获取回收订单列表
	 * @param  int $status 订单状态，-1-未完成，0-待处理，1-待接单，2-已接单，3-待确认， 4-客户取消订单，5-待回收，6-已回收，7-已完成
	 * @return array 二维数组
	 */
	public function get_order_list_by_city($status = FALSE,$city_id = FALSE){
		$condition = array();
		if($city_id){
			$condition['city_id'] = $city_id;
		}
		if($status != ''){
			$condition['status'] = $status;
		}else{
			$condition['status'] = array('egt',0);
		}

		return $this->RecycleOrder
				->join('mp_region ON mp_recycle_order.region=mp_region.re_id','LEFT')
				->join('mp_version ON mp_recycle_order.vid=mp_version.vid','LEFT')
				->join('mp_brand ON mp_version.bid=mp_brand.bid','LEFT')
				->join('mp_member ON mp_recycle_order.mid=mp_member.member_id','LEFT')
				->join('mp_city ON mp_region.city_id=mp_city.city_id','LEFT')
				->field('rid,status,order_num,update_time,valuation,price,phone_number,mp_region.re_id,mp_city.city_id,mp_region.region,address,door_time,b_name,v_name,mp_info,m_name,m_type,m_account')
				->where($condition)->order('update_time desc')->select();
	}



	/**
     * 插入或修改回收订单
     * @param $data 提交回收订单的数据
     * @param $rid 回收订单id
     */
	public function set_order($data,$rid){

		$condition['rid'] = $rid;
		return $this->RecycleOrder->where($condition)->save($data);
	}


	/**
	 * 获取回收订单
	 * @param $rid 回收订单id
	 */
	public function get_order($rid){

		$condition['rid'] = $rid;
		return $this->RecycleOrder->join('mp_version ON mp_recycle_order.vid=mp_version.vid','LEFT')
				->join('mp_region ON mp_recycle_order.region=mp_region.re_id','LEFT')
				->join('mp_city ON mp_region.city_id=mp_city.city_id','LEFT')
				->join('mp_member ON mp_recycle_order.mid=mp_member.member_id','LEFT')
				->field('rid,phone_number,repair_way,update_time,mp_recycle_order.vid,mp_info,door_time,stores,mp_city.city,mp_region.region,address,order_num,valuation,price,v_name,m_name,m_account,status')
				->where($condition)->find();
	}

}